<template>
  <div center>
    <el-form ref="info" :rules="rules" :model="info" size="mini" label-position="right"
             label-width="110px">
      <el-form-item label="检测结论">
        <el-input type="textarea" :disabled="isShow" v-model="info.shjl" style="width: 100%" :rows="10"></el-input>
      </el-form-item>
      <el-form-item label="建议">
        <el-input type="textarea" :disabled="isShow" v-model="info.jy" style="width: 100%" :rows="10"></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align: center" v-if="!isShow">
      <el-button type="primary" size="small" @click="addOrEdit">保存</el-button>
    </div>
  </div>
</template>


<script>
import imgUpdate from '@/components/Upload/imgUpdate'
import { getInfo, addOrEdit } from '@/api/table/tableOne'
import { getUserList } from '@/api/system/sysUser'
import { getUserId } from '@/utils/auth'

export default {
  components: {
    imgUpdate
  },
  props: {
    noCreated: {
      type: Boolean,
      default: null
    },
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      info: {},
      userId: getUserId(),
      userList: [],
      tableInfo: {},
      tableIndex: null,
      centerDialogVisible: false,
      centerDialogVisible2: false,
      rules: {
        jclx: [{ required: true, message: '请输入内容', trigger: 'chang' }],
        jcbh: [{ required: true, message: '请输入内容', trigger: 'chang' }],
        dcrq: [{ required: true, message: '请选择日期', trigger: 'chang' }],
        sjcdwmc: [{ required: true, message: '请输入内容', trigger: 'chang' }],
        sjcdwptr: [{ required: true, message: '请输入内容', trigger: 'chang' }],
        dcr: [{ required: true, message: '请选择内容', trigger: 'chang' }]
      }
    }
  },
  created() {
    this.getUserList()
    this.info.table1Json = []
    this.info.table2Json = []
  }, methods: {
    // 新增或编辑表1
    addTable1(index, row) {
      this.tableIndex = index
      this.tableInfo = {}
      if (row != null) {
        this.tableInfo = row
      }
      this.centerDialogVisible = true
    },
    // 保存表1
    addOrEditTable1() {
      if (this.tableIndex == null) {
        this.info.table1Json.push(this.tableInfo)
      } else {
        this.info.table1Json.splice(this.tableIndex, 1, this.tableInfo)
      }
      this.centerDialogVisible = false
    },
    // 删除表1
    delTable1(index, row) {
      this.info.table1Json.splice(index, 1)
    },
    // 新增或编辑表2
    addTable2(index, row) {
      this.tableIndex = index
      this.tableInfo = {}
      if (row != null) {
        this.tableInfo = row
      }
      this.centerDialogVisible2 = true
    },
    // 保存表1
    addOrEditTable2() {
      if (this.tableIndex == null) {
        this.info.table2Json.push(this.tableInfo)
      } else {
        this.info.table2Json.splice(this.tableIndex, 1, this.tableInfo)
      }
      this.centerDialogVisible2 = false
    },
    // 删除表1
    delTable2(index, row) {
      this.info.table2Json.splice(index, 1)
    },
    // 获取所有用户
    getUserList() {
      getUserList().then(response => {
        this.userList = response.data
      })
    },
    getInfo(id) {
      getInfo(id).then(response => {
        this.info = response.data
      })
    },
    addOrEdit: function() {
      this.$refs['info'].validate((valid) => {
        if (valid) {
          addOrEdit(this.info.id, this.info.jclx, this.info.jcbh, this.info.dcrq, this.info.sjcdwmc, this.info.sjcdwdz, this.info.hyfljdm,
            this.info.dwxz, this.info.lxr, this.info.lxdh, this.info.jcfw, this.info.zycpjcl, this.info.scgylct,
            this.info.sbbjt, this.info.scgylctImgJson, this.info.sbbjtImgJson, null, this.info.sjcdwptr, this.info.table1Json, this.info.table2Json, this.info.shjl, this.info.jy).then(response => {
            if (this.info.id == null) {
              // 新增
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
              this.$parent.$parent.$parent.$parent.$parent.getData()

            } else {
              // 编辑
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            }
            this.info = response.data
            this.$parent.$parent.$parent.$parent.$parent.centerDialogVisible2 = false
          })
        }
      })
    },
    // 初始化
    init(id) {
      if (id != null) {
        this.getInfo(id)
      } else {
        this.info = {}
        this.info.table1Json = []
        this.info.table2Json = []
      }
    }
  }
}
</script>

